package com.ctvit.solr.kit.solr.controller;

import com.alibaba.fastjson.JSONObject;
import cms.api.common.constant.SolrApiConstant;
import cms.api.common.result.ResponseResult;
import com.ctvit.solr.kit.cp.DocumentCondition;
import com.ctvit.solr.kit.cp.DocumentParam;
import com.ctvit.solr.kit.solr.service.SolrService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Objects;

/**
 * Solr控制器
 *
 * @author heyingcheng
 * @email heyingcheng@ctvit.com.cn
 * @date 2019/6/23 10:04
 */
@Slf4j
@RestController
@RequestMapping("solr")
public class SolrController {

    @Autowired
    private SolrService solrService;

    /**
     * 添加或更新文档
     *
     * @param collection    索引库
     * @param documentParam 添加或更新文档
     * @return ResponseResult
     * @author heyingcheng
     * @date 2019/6/23 21:58
     */
    @PostMapping("{collection}/document")
    @ResponseBody
    public ResponseResult document(@PathVariable("collection") String collection, @RequestBody DocumentParam documentParam) {
        JSONObject result = solrService.saveOrUpdateDocument(collection, documentParam);
        if (Objects.nonNull(result)) {
            return ResponseResult.success(result);
        } else {
            return ResponseResult.failure("添加或更新文档失败");
        }
    }

    /**
     * 查询文档
     *
     * @param collection        索引库
     * @param documentCondition 文档查询条件
     * @return ResponseResult
     * @author heyingcheng
     * @date 2019/6/23 21:58
     */
    @GetMapping("{collection}/select")
    public ResponseResult select(@PathVariable("collection") String collection, @RequestBody DocumentCondition documentCondition) {
        JSONObject result = solrService.selectDocument(collection, documentCondition);
        if (result.containsKey(SolrApiConstant.ERROR)) {
            return ResponseResult.failure(result.get(SolrApiConstant.ERROR));
        }
        return ResponseResult.success(result);
    }

    /**
     * 按ID删除文档
     *
     * @param collection 索引库
     * @param id         文档ID
     * @return ResponseResult
     * @author heyingcheng
     * @date 2019/6/23 21:59
     */
    @DeleteMapping("{collection}/delete/id")
    @ResponseBody
    public ResponseResult deleteById(@PathVariable("collection") String collection, @RequestParam("id") String id) {
        JSONObject result = solrService.deleteById(collection, id);
        if (Objects.nonNull(result)) {
            return ResponseResult.success(result);
        } else {
            return ResponseResult.failure("按ID删除文档失败");
        }
    }

    /**
     * 按ID批量删除文档
     *
     * @param collection 索引库
     * @param ids        文档IDs
     * @return ResponseResult
     * @author heyingcheng
     * @date 2019/6/23 22:00
     */
    @DeleteMapping("{collection}/delete/ids")
    @ResponseBody
    public ResponseResult deleteByIds(@PathVariable("collection") String collection, @RequestParam("id") List<String> ids) {
        JSONObject result = solrService.deleteByIds(collection, ids);
        if (Objects.nonNull(result)) {
            return ResponseResult.success(result);
        } else {
            return ResponseResult.failure("按ID批量删除文档失败");
        }
    }

    /**
     * 删除全部文档
     *
     * @param collection 索引库
     * @return ResponseResult
     * @author heyingcheng
     * @date 2019/6/23 22:00
     */
    @DeleteMapping("{collection}/delete/all")
    @ResponseBody
    public ResponseResult deleteAll(@PathVariable("collection") String collection) {
        JSONObject result = solrService.deleteAll(collection);
        if (Objects.nonNull(result)) {
            return ResponseResult.success(result);
        } else {
            return ResponseResult.failure("删除全部文档失败");
        }
    }

}
